import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import FormModal from '#cpt/temp/FormModal'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
const FillParameterModal	= $Lazy.state(()=>import('./cpt/fill-parameter-modal'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '账期',			name: 'summary_sn', 	type:'select', loading:true, width:220, mode:'multiple' },
			{ label: '受检单位',		name: 'hosp_id', 		type:'select', loading:true, width:220, mode:'multiple' },
			{ label: '业务员',		name: 'salesman_id', 	type:'select', loading:true},
			{ label: '开票单位',		name: 'organization_name' },
			{ label: '区域',			name: 'region_id', 		type:'select', loading:true, width:180, mode:'multiple' },
			{ label: '操作时间',		names: ['start_at','end_at'], name:'time-null', type:'date-range' },
		],
		// 重置数据
		formData:[
			{ label: '重置账期',		name:'summary_sn', full:true, type:'select',required: true },
		]
	}
	
	model = {}
	componentDidMount(){
		this.getSelect()
		this.getModalSelect()
		if(window.isInit){
			this.fetch()
		}
	}
	
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 账期
			$fn.setRow(forms, 'summary_sn', await f.getCache({ api:'bill-summary/select' } ) ) 
			// 业务员
			$fn.setRow(forms, 'salesman_id', await f.getCache({ api:'salesman' } ) )
			// 区域
			$fn.setRow(forms, 'region_id', await f.getCache({ api:'bs_area' } ) )
			// 医院名称
			$fn.setRow(forms, 'hosp_id', await f.getCache({ api:'hospital' } ) )
			this.setState({ forms })
		})
	}
	// 获取弹窗下拉框数据
	getModalSelect = async () => {
		const { formData } = this.state
		$fn.setModalRows(formData, 'summary_sn', ()=>{
			$cache.then( async f => {
				//账期
				$fn.setRow(formData, 'summary_sn', await f.getCache({ api:'bill-summary/select' } ) )
				this.setState({ formData })
			})
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'fi-standing-book/index')
	
	// 表单
	cols = [
		{ title: '账期', 		dataIndex: 'summary_sn', 			width:100 },
		{ title: '受检单位', 	dataIndex: 'hosp_name', 			width:200 },
		{ title: '业务员', 		dataIndex: 'salesman_user', 		...$fn.table.name },
		{ title: '开票单位', 	dataIndex: 'organization_name', 	width:200 },
		{ title: '区域', 		dataIndex: 'region_name', 			width:100 },
		{ title: '暂估金额', 	dataIndex: 'estimate_price', 		...$fn.table.num },
		{ title: '账单金额', 	dataIndex: 'bill_price', 			...$fn.table.num },
		{ title: '新冠金额', 	dataIndex: 'ncov_price', 			...$fn.table.num },
		{ title: '现金金额', 	dataIndex: 'cash_price', 			...$fn.table.num },
		{ title: '开票金额', 	dataIndex: 'invoice_price', 		...$fn.table.num },
		{ title: '不开票金额', 	dataIndex: 'not_invoice_price', 	...$fn.table.num },
		{ title: '未开票金额', 	dataIndex: 'wait_invoice_price', 	...$fn.table.num },
		{ title: '回款金额', 	dataIndex: 'collection_price', 		...$fn.table.num },
		{ title: '未回款金额', 	dataIndex: 'not_collection_price', 	...$fn.table.num },
		{ title: '欠款天数', 	dataIndex: 'arrears_date', 			...$fn.table.num },
		{ title: '操作时间', 	dataIndex: 'created_at', 			...$fn.table.time },
		TableControls.controls.call(this,{ width: 250 })
	]
	// 表格操作按钮
	table_controls = (text,{ hosp_id,uuid }) => {
		return [
			TableControls.pushTo.call(this, '账单金额', `receivable?hosp_id=${hosp_id}`),
			TableControls.pushTo.call(this, '回款调整', `receivable-records?hosp_id=${hosp_id}`),
			TableControls.pushTo.call(this, '已开发票', `invoice?hosp_id=${hosp_id}`),
		]
	}
	// 操作按钮
	controls = () => {
		const { selectedRows } = this.state
		return [
			// 导入
			Controls.imports.call(this, 'fi-standing-book/import'),
			// 
			Controls.click.call(this, '填写台账', () => this.fillParameterModal.open(selectedRows)),
			// 重置数据
			Controls.click.call(this, '生成/重置数据', () => this.modal.open({formRef: this.formRef}))
		]
	}
	
	render(){
		return (
			<>
				<Page title='台账列表' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this,{isInit:window.isInit, type:-1}) }
				</Page>
				{/* 填写台账 */}
				<FillParameterModal onRef={ref=>this.fillParameterModal = ref} onRefresh={this.fetch}/>
				{/* 重置数据 */}
				{FormModal.call(this,{ title:'生成/重置数据', labelWidth:'s5', width:300, api:'fi-standing-book/reset'})}
			</>
		)
	}
}

export default Index